from branca.element import MacroElement
from jinja2 import Template

class drawcircle(MacroElement):
    _template = Template(u"""
        {% macro script(this, kwargs) %}
                var r=0
                var i=null
                var tempCircle=new L.circle()
            
                {{this._parent.get_name()}}.on('contextmenu', onmouseDown);
                {{this._parent.get_name()}}.on('mouseup',onmouseUp);
                {{this._parent.get_name()}}.on('mousemove',onMove)
            
                function onmouseDown(e)
                {
                    i=e.latlng
                    //确定圆心
                }
                function onMove(e) {
                    if(i) {
                        r = L.latLng(e.latlng).distanceTo(i)
                        tempCircle.setLatLng(i)
                        tempCircle.setRadius(r)
                        tempCircle.setStyle({color:'red',fillColor:'yellow',fillOpacity:0.2})
                        {{this._parent.get_name()}}.addLayer(tempCircle)
            
                    }
                }
            
                function onmouseUp(e)
                {
                    r = L.latLng(e.latlng).distanceTo(i)//计算半径
                    L.circle(i,{radius:r,color:'red',fillColor:'yellow',fillOpacity:0.15}).addTo({{this._parent.get_name()}})
                    i=null
                    r=10000
                }
                
                
            {% endmacro %}
            """)
    def __init__(self, popup=None):
        super(drawcircle, self).__init__()
        self._name = 'drawcircle'

a=drawcircle()